package com.hao.hospital.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.shiro.web.tags.UserTag;

import com.hao.hospital.model.MedicalRecord;
import com.hao.hospital.model.User;
import com.hao.hospital.service.MainService;
import com.hao.hospital.service.PaymentService;
import com.hao.hospital.service.PersonService;
import com.hao.hospital.service.UserService;
import com.hao.hospital.util.TimeUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;



public class UserAction extends Controller {
	/**
	 * 更新密码
	 */
	public void updatePass(){
		int user_id=getParaToInt("user_id");
		String oldPass=getPara("oldPass");
		String newPass=getPara("newPass");
		User user=new User();
		user=user.findById(user_id);
		HashMap<String, Boolean> hm=new HashMap<>();
		if(oldPass.equals(user.getPaypass())){
			user.setPaypass(newPass);
			user.update();
			hm.put("rtn", true);
		}else{
			hm.put("rtn", false);
		}
		renderJson(hm);
	}
	/**
	 * 更新患者信息
	 */
	public void updateInfo(){
		User user=new User();
		user.setUserId(getParaToInt("user_id"));
		user.setUserName(getPara("user_name"));
		user.setAddress(getPara("address"));
		user.setPhone(getParaToLong("phone"));
		boolean flag=false;
		try {
			 flag=user.update();
			 System.out.println(flag);
		} catch (Exception e) {
			 
		}
		HashMap<String, Boolean> hashMap=new HashMap<>();
		hashMap.put("rtn", flag);
		renderJson(hashMap);
	}
	/**
	 * 获取用户信息
	 */
	public void userInfo(){
		String openid=getSessionAttr("openid");
		//String openid="oYwN7vwVkZdAS81zv45kU9eGMCKQ";
		renderJson(UserService.me().userInfo(openid));
	}
	
	public void setHeader(){
		HttpServletResponse response=getResponse();
		response.setHeader("Access-Control-Allow-Origin", "*");
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
	    response.setHeader("Access-Control-Max-Age", "3600");
	    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	}
	/**
	 * 获取个人预约记录
	 */
	public void register(){
		setHeader();
		String openid=getSessionAttr("openid");
		System.out.println(openid);
		//String openid="oYwN7vwVkZdAS81zv45kU9eGMCKQ";
		renderJson(UserService.me().register(openid));
	}
	/**
	 * 通过患者openid查询用户病历 http请求方式：get
	 */
	public void medical() {
		String openid = getPara("openid");
		Record result = new Record();
		if (StringUtils.isNotEmpty(openid)) {
			result = UserService.me().medical(openid);
		} else {
			result.set("result", "参数不合法");
		}
		renderJson(result);
	}

	/**
	 * 通过openid获取用户支付密码
	 */
	public void verifiers() {
		String openid = getPara("openid");
		System.out.println("获取支付密码openid:" + openid);
		Record result = new Record();
		if (StringUtils.isNotEmpty(openid)) {
			System.out.println("进入到获取支付密码");
			result = UserService.me().verifiers(openid);
			System.out.println("OK");
			System.out.println(result);
			System.out.println(result.get("paypass"));
			System.out.println(result.get("balance"));
			System.out.println("不Ok");
		} else {
			result.set("result", "参数不合法");
		}
		renderJson(result);
	}

	/**
	 * 1.用户账户扣除费用，2.创建支付记录，3.创建预约记录,4.预约数量减少
	 * 
	 * @throws ParseException
	 */
	@Before(Tx.class)
	public void buildRecord() throws ParseException {
		String openid = getPara("openid");
		int person_id = getParaToInt("person_id");
		Long worktime = getParaToLong("worktime");
		int money = getParaToInt("money");
		System.out.println(openid);
		System.out.println(person_id);
		System.out.println(worktime);
		System.out.println(money);
		Map<String, Object> result = new HashMap<>();
		if (UserService.me().isUserRegister(openid, worktime) >= 1) {
			result.put("rtnmsg", "请不要重复预约");
			renderJson(result);
			return;
		}
		if (PersonService.me().GetRegisterNum(person_id, worktime) <= 0) {
			result.put("rtnmsg", "预约余量不足");
			renderJson(result);
			return;
		}
		try {
			// 1.扣除支付费用
			UserService.me().DeductMoney(openid, money);
			System.out.println("-----1-----");
			// 2.创建支付记录
			int user_id = UserService.me().getUserIdByOpenId(openid).getInt("user_id");
			System.out.println("-----2-----");
			long createTime = System.currentTimeMillis();
			boolean flag = PaymentService.me().addPayRecord(user_id, money, createTime,1);
			System.out.println("-----3-----");
			System.out.println("flag:" + flag);
			System.out.println(user_id);
			if (flag == true) {
				// 3.创建预约记录
				System.out.println("进入到第四步");
				flag = PaymentService.me().addRegisterRecord(user_id, person_id, worktime, createTime);
				
				System.out.println("-----4-----");
				if (flag == true) {
					// 4.预约数量减少
					flag = PersonService.me().updateRegisterNum(person_id, worktime);
					
					System.out.println("-----5-----");
					if (flag == true) {
						String send= MainService.me().sendShortMessage(user_id,TimeUtil.me().stampToDateInYearNoWeek(worktime.toString()) , person_id);
						System.out.println(send);
						result.put("rtnmsg", true);
						renderJson(result);
					}
				}
			}
		} catch (Exception e) {
			result.put("rtnmsg", false);
			renderJson(result);
		}

	}
	
	/**
	 * 通过患者id查询用户病历 http请求方式：get
	 */
	public void bill() {
		
	}

	/**
	 * 缴费管理获取到所有患者缴费信息 http请求方式：get
	 */
	public void paymentlist() {
		int pageNumber = getParaToInt("pageNumber");
		renderJson(UserService.me().paymentlist(pageNumber));
	}

	/**
	 * 缴费管理获取到所有患者缴费信息 http请求方式：get
	 */
	public void unpaymentlist() {
		int pageNumber = getParaToInt("pageNumber");
		renderJson(UserService.me().unpaymentlist(pageNumber));
	}

	/**
	 * 获取所有患者信息 http请求方式：get
	 */
	public void allinfo() {
		int pageNumber = getParaToInt("pageNumber");
		renderJson(UserService.me().allinfo(pageNumber));
	}

	/**
	 * 患者对账户进行充值 http请求方式：post (未测试)
	 */
	@Before(POST.class)
	public void recharge() {
		//String name = getPara("name");
		int user_id=getParaToInt("user_id");
		Integer balance = getParaToInt("balance");
		Map<String, String> data = new HashMap<String, String>();
		int flag = UserService.me().recharge(user_id, balance);
		//充值记录
		PaymentService.me().addPayRecord(user_id,balance, System.currentTimeMillis(), 2);
		if (flag == 1) {
			data.put("rtnmsg", "ok");
		} else if (flag == 0) {
			data.put("rtnmsg", "false");
		}
		renderJson(data);
	}

	/**
	 * 通过姓名获取用户预交费信息
	 */
	public void unpayment() {
		String name = getPara("name");
		renderJson(UserService.me().unpayment(name));
	}
}
